package com.testapp.bluetoothtest08;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.testapp.bluetoothtest08.Constant.Constants;
import com.testapp.bluetoothtest08.File.Statement;
import com.testapp.bluetoothtest08.Home.HomeFragment;
import com.testapp.bluetoothtest08.Judge.IsYellow;
import com.testapp.bluetoothtest08.My.MyFragment;

public class HomeActivity extends AppCompatActivity implements View.OnClickListener {
    private LinearLayout home, my;
    public ImageView home_iv, my_iv, iCurrent;

    private Fragment mainHome;
    private Fragment mainMy;

    private String username;


    private Intent intent = null;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        init();

    }

    @Override
    protected void onStart() {
        super.onStart();
        if(!Constants.login){
            logout();
        }
    }



    private void init() {
        home = findViewById(R.id.home);
        my = findViewById(R.id.my);
        home.setOnClickListener(this);
        my.setOnClickListener(this);

        home_iv = findViewById(R.id.home_img);
        my_iv = findViewById(R.id.my_img);


        mainHome = new HomeFragment();
        mainMy = new MyFragment();

        replaceFragment(mainHome);
        iCurrent = home_iv;
        iCurrent.setSelected(true);

        //定时请求
        requestServerScanMachineNum.start();

        //隔段时间求情是否接触
        requestYellow.start();



    }
    Thread requestYellow = new Thread(new Runnable() {
        @Override
        public void run() {
            while(true){
                new IsYellow().judge();
                try {
                    Thread.sleep(60*1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    });

    Thread requestServerScanMachineNum = new Thread(new Runnable() {
        @Override
        public void run() {

            while(true){
                try {
                    new IsYellow().judgeScanMachine();
                    Thread.sleep(1000*60);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    });

    @Override
    public void onClick(View view) {
        iCurrent.setSelected(false);
        switch (view.getId()) {
            case R.id.MainActivity_Home:
            case R.id.home:
                replaceFragment(mainHome);
                iCurrent = home_iv;
                iCurrent.setSelected(true);
                break;
            case R.id.MainActivity_My:
            case R.id.my:
                replaceFragment(mainMy);
                iCurrent = my_iv;
                iCurrent.setSelected(true);
                break;

        }
    }


    @Override
    protected void onDestroy() {
        requestServerScanMachineNum.interrupt();
        requestYellow.interrupt();
        super.onDestroy();
    }

    private void replaceFragment(Fragment fragment) {
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        ft.replace(R.id.MainActivity_Fragment, fragment);
        //ft.addToBackStack(null);
        /*
            把每个fragment放入栈中，按一下返回取出一个栈
        */
        ft.commit();
    }

    public void logout() {
        intent = new Intent(this, MainActivity.class);
        if(Constants.login==false){
            Log.d("flag ---> ","false");
        }
        startActivity(intent);
    }
}